package com.nodecollege.cloud.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nodecollege.cloud.client.FastDFSClient;
import com.nodecollege.cloud.common.exception.UpmsException;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryVO;
import com.nodecollege.cloud.common.model.po.UpmsFile;
import com.nodecollege.cloud.common.utils.NCUtils;
import com.nodecollege.cloud.dao.mapper.UpmsFileMapper;
import com.nodecollege.cloud.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 文件处理
 *
 * @author LC
 * @date 2020/3/21 15:28
 */
@Slf4j
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FastDFSClient fastDFSClient;

    @Autowired
    private UpmsFileMapper fileMapper;

    /**
     * 上传文件
     *
     * @param file     文件
     * @param upmsFile 文件信息
     */
    @Override
    public UpmsFile uploadFile(MultipartFile file, UpmsFile upmsFile) {
        NCUtils.nullOrEmptyThrow(file, new UpmsException("-1", "参数缺失！"));
        checkFile(upmsFile);
        String filePath = fastDFSClient.uploadFile(file);
        upmsFile.setFilePath(filePath);
        return insertFile(upmsFile);
    }

    /**
     * 上传文件并生成缩略图
     *
     * @param file     文件
     * @param upmsFile 文件信息
     */
    @Override
    public UpmsFile uploadFileThumb(MultipartFile file, UpmsFile upmsFile) {
        NCUtils.nullOrEmptyThrow(file, new UpmsException("-1", "参数缺失！"));
        checkFile(upmsFile);
        String filePath = fastDFSClient.uploadFileThumb(file);
        upmsFile.setFilePath(filePath);
        return insertFile(upmsFile);
    }

    /**
     * 查询文件列表 分页
     *
     * @param queryVO
     * @return
     */
    @Override
    public NCResult<UpmsFile> getFileListPage(QueryVO<UpmsFile> queryVO) {
        Page page = PageHelper.startPage(queryVO.getPageNum(), queryVO.getPageSize());
        List<UpmsFile> list = fileMapper.selectListByMap(queryVO.toMap());
        return NCResult.ok(list, page.getTotal());
    }

    /**
     * 查询文件列表
     *
     * @param queryVO
     * @return
     */
    @Override
    public List<UpmsFile> getFileList(QueryVO<UpmsFile> queryVO) {
        return fileMapper.selectListByMap(queryVO.toMap());
    }

    /**
     * 校验文件信息
     *
     * @param upmsFile
     */
    private void checkFile(UpmsFile upmsFile) {
        NCUtils.nullOrEmptyThrow(upmsFile, new UpmsException("-1", "参数缺失！"));
        NCUtils.nullOrEmptyThrow(upmsFile.getUserId(), new UpmsException("-1", "用户id不能为空！"));
        NCUtils.nullOrEmptyThrow(upmsFile.getFileType(), new UpmsException("-1", "文件类型不能为空！"));
    }

    /**
     * 插入记录
     *
     * @param upmsFile
     * @return
     */
    private UpmsFile insertFile(UpmsFile upmsFile) {
        upmsFile.setCreateTime(new Date());
        if (NCUtils.isNullOrEmpty(upmsFile.getFileName())) {
            upmsFile.setFileName(upmsFile.getFilePath());
        }
        fileMapper.insertSelective(upmsFile);
        return upmsFile;
    }

    /**
     * 删除文件
     *
     * @param upmsFile 文件信息
     */
    @Override
    public void delFile(UpmsFile upmsFile) {
        NCUtils.nullOrEmptyThrow(upmsFile, new UpmsException("", "参数缺失！"));
        NCUtils.nullOrEmptyThrow(upmsFile.getFileId(), new UpmsException("", "文件主键不能为空！"));
        if (NCUtils.isNullOrEmpty(upmsFile.getUserId()) && NCUtils.isNullOrEmpty(upmsFile.getTenantId())) {
            throw new UpmsException("-1", "用户id或者租户id不能为空！");
        }
        UpmsFile ex = fileMapper.selectByPrimaryKey(upmsFile.getFileId());
        if (ex == null) {
            // 文件不存在
            return;
        }
        if (NCUtils.isNotNullOrNotEmpty(ex.getTenantId())) {
            if (!ex.getTenantId().equals(upmsFile.getTenantId())) {
                throw new UpmsException("-1", "无权删除该文件！");
            }
        } else {
            if (!ex.getUserId().equals(upmsFile.getUserId())) {
                throw new UpmsException("-1", "只能删除自己的文件数据！");
            }
        }
        fileMapper.deleteByPrimaryKey(upmsFile.getFileId());
        fastDFSClient.deleteFile(ex.getFilePath());
    }
}
